#!/usr/bin/perl

use strict;
use warnings;

sub parse_tag_begin {
    my ($tag, $section_level) = @_;
    if ($tag eq "section") {
        $section_level ++;
    } elsif ($tag eq "chapter") {
        $section_level = 1;
    }
    return $section_level;
}

sub parse_tag_partial {
    my ($tag, $section_level, $text) = @_;
    my $new_text = "";
    if ($tag eq "para") {
        while ($text =~ s/^ //mg) {};
        while ($text =~ s/\n\n/\n/mg) {};
        $new_text = $text;
        $text = "";
    } elsif ($tag eq "listitem") {
        $new_text = " ";
    }
    return ($new_text, $section_level, $text);
}

sub parse_tag_finished {
    my ($tag, $text, $section_level) = @_;
    my $new_text = "";
    
    if ($tag eq "para") {
        while ($text =~ s/^ //mg) {};
        while ($text =~ s/\n\n/\n/mg) {};
        $new_text = $text . "\n\n";
    } elsif ($tag eq "uri") {
        $new_text = $text;
    } elsif ($tag eq "command" or $tag eq "code") {
        while ($text =~ s/^ //mg) {};
        while ($text =~ s/\n\n/\n/mg) {};
        $new_text = "`$text`";
    } elsif ($tag eq "screen") {
        $new_text = "\n{{{\n$text\n}}}\n";
    } elsif ($tag eq "title" and $section_level > 0) {
        for(my $i = 0; $i < $section_level; $i++) {
            $new_text .= '=';
        }
        $new_text .= $text;
        for(my $i = 0; $i < $section_level; $i++) {
            $new_text .= '=';
        }
        $new_text .= "\n";
    } elsif ($tag eq "chapter") {
        $new_text = "%%%%%%%%\n";
    } elsif ($tag eq "term") {
        $new_text = "*$text*\n";
    }
    return ($new_text, $section_level);
}

sub recurse {

    # recurse through the book text.
    # this function should return when the ending tag $tag is seen.
    my ($text, $my_tag, $section_level) = @_;

    my $new_text = "";

    ($section_level) = parse_tag_begin($my_tag, $section_level);

    my $full_before = "";

    while (1) {
        # Find the first tag.
        $text =~ m/<(.*?)>/;
        my $tag = $1;
        my $before = $`;
        my $after = $'; #'

        # Was it an ending tag?
        if ($tag =~ m/^\//) {
            # Do tag-specific stuff.
            my $stripped_tag = $tag;
            $stripped_tag =~ s/\///;
            $full_before .= $before;
            my ($new_new_text, $new_section_level) = parse_tag_finished($stripped_tag, $full_before, $section_level);
            $section_level = $new_section_level;
            $new_text .= $new_new_text;


            return ($new_text, $after);
        } else {
            $full_before .= $before;
            my ($new_new_text, $new_section_level, $new_old_text) = parse_tag_partial($my_tag, $section_level, $before);
            $new_text .= $new_new_text;
            $section_level = $new_section_level;
            $full_before = $new_old_text;
            # recurse.

            my ($parsed_text, $rest) = recurse($after, $tag, $section_level);
            $text = $rest;
            $new_text .= $parsed_text;
        }
        
    }
}

my ($img_root, $source_file, $dest_dir) = @ARGV;

local $/ = undef;
open(my $fd, "<", $source_file) or die ("couldnt open source file");
my $text = <$fd>;
close($fd);

$text =~ s/<\?.*?\?>//;
my @parsed_arr = recurse($text, '', 0);

my $parsed = $parsed_arr[0];

# Go through looking for '%%%%%%%' and splitting chapters there.
my @chapters;
my @chapters_names;

my $i = 0;

while($parsed =~ m/%%%%%%%%/g) {
    $chapters[$i] = $`;
    $parsed = $'; #'

    $chapters[$i] =~ m/[^=]?=(.*?)=[^=]/m;

    $chapters_names[$i] = $1;
    $chapters_names[$i] =~ s/(\w+)/\u\L$1/g; # force words to title case.
    $chapters_names[$i] =~ s/ //g; # remove all spaces.
    $i++;
}

for(my $i = 0; $i < scalar(@chapters); $i++) {
    open (my $fd, ">", "$dest_dir/$chapters_names[$i].wiki");
    print $fd $chapters[$i];
    close ($fd);
}

# get the book's name: this will be in the first <title> tag.
$text =~ m/<title>(.*?)<\/title>/;
my $book_name = $1;
# make it wiki-ified.
my $book_name_wiki = $book_name;
$book_name_wiki =~ s/(\w+)/\u\L$1/g; # force words to title case.
$book_name_wiki =~ s/ //g; # remove all spaces.

# make an index page.
my $index = "=$book_name=\n";
foreach my $name (@chapters_names) {
    $index .= " * $name\n";
}

# print the index.
open ($fd, ">", "$dest_dir/$book_name_wiki.wiki");
print $fd $index;
close($fd);
